package com.crl.rcdp.sys.dao;

import com.cars.util.string.StringUtil;
import com.crl.dao.BaseDao;
import com.crl.rcdp.sys.model.SysApitype;
import com.crl.rcdp.sys.vo.SysApitypeSearchVO;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @description: 系统管理-接口类型Dao
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020-07-30 22:43:56
 */
@Repository
public class SysApitypeDao2 extends BaseDao<SysApitype, SysApitypeSearchVO> {
    /**
     * 新增
     *
     * @param sysApitype
     * @return
     */
    public int add(SysApitype sysApitype) {
        String sql = "INSERT INTO td_sys_apitype(ID,NAME,REMARKS) " +
                "VALUES(seq_td_sys_apitype.nextval,:name,:remarks)";
        return insert(sql, sysApitype);
    }

    /**
     * 修改
     *
     * @param sysApitype
     * @return
     */
    public int update(SysApitype sysApitype) {
        String sql = "UPDATE td_sys_apitype SET NAME=:name,REMARKS=:remarks " +
                "WHERE ID=:id";
        return update(sql, sysApitype);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    public int delete(Long id) {
        String sql = "DELETE FROM td_sys_apitype WHERE ID=?";
        return delete(sql, id);
    }

    /**
     * 列表
     *
     * @param sysApitypeSearchVO
     * @return
     */
    public List<SysApitype> list(SysApitypeSearchVO sysApitypeSearchVO) {
        String sql = "SELECT ID,NAME,REMARKS FROM td_sys_apitype WHERE 1=1 ";
        sql += createSearchSQL(sysApitypeSearchVO);
        sql += " ORDER BY ID ASC";
        return list(sql, sysApitypeSearchVO);
    }

    /**
     * 查询数据条数
     *
     * @param sysApitypeSearchVO
     * @return
     */
    public int count(SysApitypeSearchVO sysApitypeSearchVO) {
        String sql = "SELECT COUNT(ID) FROM td_sys_apitype WHERE 1=1 ";
        sql += createSearchSQL(sysApitypeSearchVO);
        return count(sql, sysApitypeSearchVO);
    }

    /**
     * 拼接查询条件
     *
     * @param sysApitypeSearchVO
     * @return
     */
    private String createSearchSQL(SysApitypeSearchVO sysApitypeSearchVO) {
        String sql = "";
        if (sysApitypeSearchVO.getId() != null) {
            sql += " AND ID =:id ";
        }
        if (StringUtil.isNotNullOrEmpty(sysApitypeSearchVO.getName())) {
            sql += " and name like '%'||:name||'%'";
        }
        return sql;
    }

    /**
     * 获取单个
     *
     * @param id
     * @return
     */
    public SysApitype get(Long id) {
        String sql = "SELECT ID,NAME,REMARKS FROM td_sys_apitype WHERE ID=?";
        return get(sql, id);
    }

    /**
     * 列表
     *
     * @return
     */
    public List<SysApitype> listAll() {
        String sql = "SELECT ID,NAME,REMARKS FROM td_sys_apitype WHERE 1=1 ";
        sql += " ORDER BY ID ASC";
        return list(sql);
    }
}
